package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.AdminBO;
import com.lwc.cfdns.pojo.dto.ChangeStatusDTO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.AdminDTO;
import com.lwc.cfdns.pojo.dto.system.AdminPwdDTO;
import com.lwc.cfdns.pojo.entity.SystemAdmin;
import com.lwc.cfdns.pojo.vo.system.AdminVO;
import com.lwc.cfdns.service.impl.SystemAdminServiceImpl;
import com.lwc.cfdns.utils.file.StorageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * 管理员
 *
 * @author luwc
 * @title AdminController
 * @description
 * @time 2023/5/16 14:42:08
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class AdminController {
    @Autowired
    private SystemAdminServiceImpl systemAdminService;

    /**
     * 创建
     *
     * @param adminDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 10:42:32
     */
    @PreAuthorize("hasAnyAuthority('system:admin:create')")// 权限控制
    @PostMapping("/admin")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody AdminDTO adminDTO) {
        return systemAdminService.create(adminDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 10:45:40
     */
    @PreAuthorize("hasAnyAuthority('system:admin:view')")
    @GetMapping("/admin/{id}")
    public AdminVO view(@PathVariable("id") @NotBlank(message = "管理员ID不能为空") Long id) {
        return systemAdminService.view(id);
    }

    /**
     * 更新
     *
     * @param adminDTO
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 10:56:54
     */
    @PreAuthorize("hasAnyAuthority('system:admin:view')")
    @PutMapping("/admin/{id}")
    public Map<String, Object> update(@Validated(ValidGroup.Update.class) @RequestBody AdminDTO adminDTO, @PathVariable("id") @NotBlank(message = "管理员ID不能为空") Long id) {
        return systemAdminService.update(id, adminDTO);
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 10:58:50
     */
    @PreAuthorize("hasAnyAuthority('system:admin:delete')")
    @DeleteMapping("/admin/{ids}")
    public Map<String, Object> delete(@PathVariable("ids") @NotBlank(message = "管理员ID不能为空") String ids) {
        return systemAdminService.delete(ids);
    }

    /**
     * 列表
     *
     * @param adminBO
     * @return IPage<SystemAdmin>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 15:10:36
     */
    @PreAuthorize("hasAnyAuthority('system:admin:lists')")
    @GetMapping("/admin")
    public IPage<SystemAdmin> lists(AdminBO adminBO) {
        return systemAdminService.lists(adminBO);
    }

    /**
     * 状态修改
     *
     * @param changeStatusDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:59
     */
    @PutMapping("/admin/changeStatus")
    public Map<String, Object> changeStatus(@RequestBody ChangeStatusDTO changeStatusDTO) {
        return systemAdminService.changeStatus(changeStatusDTO);
    }

    /**
     * 密码修改
     *
     * @param adminDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:59
     */
    @PreAuthorize("hasAnyAuthority('system:admin:resetPwd')")
    @PutMapping("/admin/resetPwd")
    public Map<String, Object> resetPwd(@RequestBody AdminDTO adminDTO) {
        return systemAdminService.resetPwd(adminDTO);
    }

    /**
     * 获取登录的用户信息
     *
     * @param
     * @return AdminVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 22:59
     */
    @GetMapping("/admin/profile")
    public AdminVO profile() {
        return systemAdminService.loginView();
    }

    /**
     * 修改当前用户资料
     *
     * @param adminDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:59
     */
    @PutMapping("/admin/profile")
    public Map<String, Object> profile(@RequestBody AdminDTO adminDTO) {
        return systemAdminService.profile(adminDTO);
    }


    /**
     * 修改当前用户密码
     *
     * @param adminPwdDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:59
     */
    @PutMapping("/admin/profile/resetPwd")
    public Map<String, Object> profileResetPwd(@RequestBody AdminPwdDTO adminPwdDTO) {
        return systemAdminService.profileResetPwd(adminPwdDTO);
    }

    /**
     * 修改当前用户头像
     *
     * @param avatarfile
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/7 20:30
     */
    @PostMapping("/admin/profile/avatar")
    public Map<String, Object> profileAvatar(MultipartFile avatarfile) {
        // 保存图片
        String imageUrl = StorageUtils.uploadImage(avatarfile);
        return systemAdminService.profileAvatar(imageUrl);
    }
}
